package cn.tsinghua.comsmacitccntsinghua.model;

import android.content.Context;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;

import java.sql.Time;
import java.util.Timer;
import java.util.TimerTask;

public class TimeTaskControl {
    //120秒后退出
    private Context context;
    private Handler handler;
    private CountDownTimer timer;//此timer记录登陆后无操作退出时间
    private CountDownTimer sendDeviceStatusTimer;

    public TimeTaskControl(Context context, Handler handler ) {
        this.context = context;
        this.handler = handler;
    }

    //设备状态定时循环上传
    public void startDeviceStatusTimer(){
        TimerTask timerTask = new TimerTask() {
            @Override
            public void run() {
                Message message = Message.obtain();
                message.what = 300001;
                handler.sendMessage(message);
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask,8*1000,30*1000); //两分钟上传一次
    }


    public void startNoIdTimer(){
        endTimer();//取消掉重新开始
        timer = new CountDownTimer(30*1000,1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                Bundle bundle = new Bundle();
                bundle.putLong("time", millisUntilFinished);
                Message message = Message.obtain();
                message.setData(bundle);
                message.what = 0x31;
                handler.sendMessage(message);
                // textView.setText(millisUntilFinished / 1000+"秒后作自动退出");
            }
            @Override
            public void onFinish() {
                Message message = Message.obtain();
                message.what = 0x20;
                handler.sendMessage(message);
            }
        };
        timer.start();
    }

    //扫码登录的用户
    public void startTimer(){
        endTimer();
        timer = new CountDownTimer(120*1000,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                Bundle bundle = new Bundle();
                bundle.putLong("time", millisUntilFinished);
                Message message = Message.obtain();
                message.setData(bundle);
                message.what = 0x30;
                handler.sendMessage(message);
               // textView.setText(millisUntilFinished / 1000+"秒后作自动退出");
            }
            @Override
            public void onFinish() {
                Message message = Message.obtain();
                message.what = 0x20;
                handler.sendMessage(message);
            }
        };
        timer.start();
    }

    public void endTimer(){
        if (null!=timer){
            timer.cancel();
        }
    }


}
